//#include "server_proxy.h"
//#include "socket.h"
//#include "buffer.h"
#include <signal.h>
#include "dce.h"
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include "group_id_message.h"
#include "gf_data_message.h"
#include "lf_data_message.h"
#include "text_label_message.h"
#include "color_label_message.h"
#include "npx_message.h"
#include "image_quality_message.h"
#include "image_splicing_message.h"
#include "a_image_message.h"
#include "dominant_color_message.h"

#include "TVMerage_SpiderAPI.h"
#include "NiuPixianAPI.h"

#include "socket.h"
#include "Log.h"

using namespace std;

int read_image(const char *jpgfile, void *&buf)
{
	int fd = open( jpgfile, O_RDONLY );
	if( fd<0)
	{
		printf("Test image does not exist.\n");
		return -1;
	}
	struct stat  len_buf;
	fstat(fd, &len_buf);
	close(fd);
	int imageSize = len_buf.st_size;

	FILE *fp = fopen(jpgfile, "rb");
	buf = new unsigned char *[imageSize];
	fread(buf, 1, imageSize, fp);
	fclose(fp);
	return imageSize;
}
int read_image_r(const char *jpgfile, void *&buf)
{
	int fd = open( jpgfile, O_RDONLY );
	if(fd<0)
	{
		printf("Test image does not exist.\n");
		return -1;
	}
	struct stat  len_buf;
	fstat(fd, &len_buf);
	int imageSize = len_buf.st_size;
	buf = new unsigned char *[imageSize];
	read(fd , buf, imageSize);
	close(fd);
	return imageSize;
}

void sig_stop(int signo){};
int g_temp_count = 9;
int main(int argc, char **argv)
{

	address addr("199.155.122.11", 50075);
	server_proxy svr(addr);
	init_logger("log_test", "TRACE");
/*
//test for connect
	int i = 0;
	while(1){
		if(svr.reconnect() == 0)
			break;
		i++;
		cout <<"times " << i << endl;
	}
*/	
//
	//test for check
/*	
	int test_fd = socket(PF_INET, SOCK_STREAM, 0);
	cout << "test_fd " << test_fd << " check : " << check_alive(test_fd) << endl;
	char *msg = "xxx";
	int bytes_sent = 0;
	signal(SIGPIPE,SIG_IGN);

	struct sockaddr_in dest_addr;
	dest_addr.sin_family = AF_INET;
	dest_addr.sin_port = htons(50075);
	dest_addr.sin_addr.s_addr = inet_addr("10.1.0.11");
	bzero(&(dest_addr.sin_zero), 8); 

	int res = connect(test_fd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in));

	if (res == -1)
	{
		perror("connect()");
		exit(1);
	}
	//bytes_sent = send(test_fd, msg, 2, 0);
	//cout << "send bytes: " << bytes_sent << endl;
	bytes_sent = 0; 
	bytes_sent = write(test_fd, msg, 2);
	cout << "write bytes 1:" << bytes_sent << endl;
	sleep(10);
	cout << "start write 2" << endl;
	bytes_sent = 0; 
	bytes_sent = write(test_fd, msg, 2);
	cout << "write bytes 2:" << bytes_sent << endl;
*/
//end of test
//
	server_proxy::io_read_timeout = 30000;
	server_proxy::io_write_timeout = 30000;
	if(argc < 3){
		cout <<"input wrong!" << endl;
		return -1;
	}
/*
	FILE *plist = fopen("pic_list.txt", "r");
	string filename = "pic_list.txt";
	string lines;
	vector<string > pic_list;
	ifstream in(filename.c_str());
	if(in){
		while(getline (in , lines)){
			pic_list.push_back(lines);
		}
	}else{
		cout << "open failed" << endl;
	}

	cout << pic_list.front() << endl;

	void *image_buf;
	
	int image_size = read_image(pic_list.front().c_str(), image_buf);
*/	
	void *image_buf;
	//int image_size = read_image("test.jpg", image_buf);
	int image_size = read_image_r( argv[2], image_buf);

	if (image_size <= 0) {
		cout << "bad jpg file" << endl;
		return -1;
	}

	timeval beg, end;
	gettimeofday(&beg, 0);
	switch (atoi(argv[1])) {
	case 0:
		{
			group_id_request_message grp_req;
			group_id_response_message grp_res;

			grp_req.set_image_id( atoi(argv[3]));
			grp_req.set_class_id(16);
			grp_req.set_subclass_id(160);
			grp_req.set_image_data_size(image_size);
			grp_req.set_image_data(image_buf);

			if (svr.request(grp_req, grp_res) != 0 || grp_res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			grp_res.show();
			break;
		}
	case 2:
		{
			gf_data_request_message gf_req;
			gf_data_response_message gf_res;

			gf_req.set_sn_high(1234567);
			gf_req.set_sn_low(1100007);
			gf_req.set_image_data_size(image_size);
			gf_req.set_image_data(image_buf);

			if (svr.request(gf_req, gf_res) != 0 || gf_res.unnormal() ) {
			cout << "request error" << endl;
			return -1;
			}
			gf_res.show();
			break;
		}
	case 1:
		{
			lf_data_request_message lf_req;
			lf_data_response_message lf_res;
			lf_req.set_image_data_size(image_size);
			lf_req.set_image_data(image_buf);

			if (svr.request(lf_req, lf_res) != 0 || lf_res.unnormal() ) {
			cout << "request error" << endl;
			return -1;
			}
			lf_res.show();
			break;
		}
	case 4:
		{
			color_label_request_message cl_req;
			color_label_response_message cl_res;
			cl_req.set_image_data_size(image_size);
			cl_req.set_image_data(image_buf);

			if (svr.request(cl_req, cl_res) != 0 || cl_res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			cl_res.show();
			break;
		}

	case 5:
		{
			npx_request_message npx_req;
			npx_response_message npx_res;
			npx_req.set_image_data_size(image_size);
			npx_req.set_image_data(image_buf);

			if (svr.request(npx_req, npx_res) != 0 ||npx_res.unnormal() ) {
				cout << "request error" << endl;
				return -1;
			}
			npx_res.show();
			break;
		}
	case 6:
		{
			image_quality_request_message iq_req;
			image_quality_response_message iq_res;
			iq_req.set_image_data_size(image_size);
			iq_req.set_image_data(image_buf);

			if (svr.request(iq_req, iq_res) != 0 || iq_res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			iq_res.show();
			break;
		}
	case 7:
		{
			image_splicing_request_message req;
			image_splicing_response_message res;
			req.set_image_id(123456789);
			req.set_class_id(10010);
			req.set_image_data_size(image_size);
			req.set_image_data(image_buf);

			if (svr.request(req, res) != 0 || res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			res.show();
			break;
		}
	case 8:
		{
			a_image_request_message req;
			a_image_response_message res;
			req.set_image_id(123456789);
			req.set_class_id(10010);
			req.set_image_data_size(image_size);
			req.set_image_data(image_buf);

			if (svr.request(req, res) != 0 || res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			res.show();
			break;
		}
	case 9:
		{
			dominant_color_request_message req;
			dominant_color_response_message res;
			req.set_image_id(123456789);
			req.set_class_id(10010);
			req.set_image_data_size(image_size);
			req.set_image_data(image_buf);

			if (svr.request(req, res) != 0 || res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			res.show();
			break;
		}


	case 3:
		{
			int text_count = argc - 5;
			int text_types[5] = { 0, 1, 2, 3, 4};
			/*
			char texts[5][256] = { "", "", "", "", "" };
			for (int i = 0; i != text_count; i++) {
				memcpy(texts[i], argv[5 + i], strlen(argv[5+i]));
			}
			*/
			char texts[5][256] = {"高跟","雪地靴","黑色", "斑点", "花边"};
			
			/*
			char texts_1[5][256] = {
				"黑色斑点花边雪地靴高跟长袖立领",
				"条纹格子纯色卡通拼色",
				"消息状态，请求和通知类型中取值0",
				"协议版本号，三位数字",
				"小白兔毛是白的马海毛"
			};
			*/
			char texts_1[5][256] = {
				"SZ3111韩版修身显瘦拼皮长袖连衣裙（黑色\\杏色）sy",
				"雪地靴",
				"豹纹",
				"消息状态，请求和通知类型中取值0",
				"协议版本号，三位数字",
			};
			char texts_2[5][256] = {"长袖","立领","黑色", "斑点", "花边"};
			
			strTextInfo *text = new strTextInfo[5];
			//cout << "size of strtextInfo:" << sizeof(strTextInfo) << endl;
			memset(text, 0, 5 * sizeof(strTextInfo) );
			for(int i = 0; i != 5; i++){
				text[i].type_source = 0;
				memcpy(text[i].text, texts_1[i], 256);
		//the input texts
			}
		
			for(int i = 0; i != 5; i++){
				cout << "type:" << text[i].type_source
					<< "; text:" << text[i].text << endl; 
			}

			text_label_request_message tl_req;
			text_label_response_message tl_res;
			
			tl_req.set_text_count(1);
			tl_req.set_texts(text);
			tl_req.set_class_id(1);
			tl_req.set_subclass_id(70020);
			//tl_req.set_text_data_size(5 * 256);
			//tl_req.set_text_data(texts);
			
			if (svr.request(tl_req, tl_res) != 0 || tl_res.unnormal()) {
				cout << "request error" << endl;
				return -1;
			}
			tl_res.show();
			break;
		}
	}
	gettimeofday(&end, 0);
	cout << "vs serve time: " << end.tv_usec - beg.tv_usec + (end.tv_sec - beg.tv_sec) * 1000000 << " us" << endl;
	return 0;
}
